package com.yunhe.sms.controller;

import com.yunhe.commens.dto.JsonResponce;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @author LeiPeifeng
 * @version V1.0
 * @date 2017/11/14 0014 15:37
 * @Description: 上传和下载
 */
@Controller
@RequestMapping
public class Storgedown {

    /**
     * 上传
     * @param req
     * @throws IOException
     * @throws ServletException
     */
    @RequestMapping("/upload")
    @ResponseBody
    public JsonResponce upload(HttpServletRequest req) throws IOException, ServletException {
        List<String> list = new ArrayList<> ();//存放文件名集合
        Collection<Part> parts = req.getParts ();//获取上传路径的集合
        for (Part part:parts){
            String fileName = part.getSubmittedFileName ();//获取上传文件的原始文件名
            String substring = fileName.substring ( fileName.lastIndexOf ( "." ) );//获取上传文件名后缀
            String newfilename = System.currentTimeMillis () + substring;//时间戳和后缀名组成新的文件名
            File file = new File ( "E:\\fs\\" );
            if (!file.isDirectory ()){
                file.mkdirs ();
            }
            part.write ( file +File.separator+newfilename);//上传的路径
            list.add ( newfilename );
        }
        return new JsonResponce ( list );
    }

    /**
     * 图片下载
     * @param req
     * @param resp
     * @throws IOException
     */
    @RequestMapping("/downimg")
    public void down(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String path = req.getParameter ( "path" );
        if (null != path && !path.isEmpty ()){
            File file = new File ( "E:\\fs\\"+path );
            if (file.exists ()){
                FileInputStream fileInputStream = new FileInputStream ( file );//从硬盘中读取
                ServletOutputStream outputStream = resp.getOutputStream ();//获得输出流
                byte[] bytes=new byte[1024*1024];
                int len;
                if ((len=fileInputStream.read (bytes))!=-1){
                    outputStream.write ( bytes,0,len );
                }
                outputStream.flush ();
                outputStream.close ();
            }
        }
    }

    /**
     * 以附件的形式下载
     * @param req
     * @param resp
     * @throws IOException
     */
    @RequestMapping("/download")
    public void download(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String path = req.getParameter ( "path" );
        if (null != path && !path.isEmpty ()){
            File file = new File ( "E:\\fs\\"+path );
            String encode = URLEncoder.encode ( path, "utf-8");//文件名转为中文,解决中文名称的乱码问题：空格识别为+
            resp.setHeader ( "content-disposition","attachment;filename="+ encode );//让浏览器已附件的形式下载文件，并指定文件名称
            if (file.exists ()){
                FileInputStream fileInputStream = new FileInputStream ( file );//从硬盘中读取
                ServletOutputStream outputStream = resp.getOutputStream ();//获得输出流
                byte[] bytes=new byte[1024*1024];
                int len;
                if ((len=fileInputStream.read (bytes))!=-1){
                    outputStream.write ( bytes,0,len );
                }
                outputStream.flush ();
                outputStream.close ();
            }
        }
    }
}
